//获取应用实例
var app = getApp();
var util = require('../../util.js');
Page({

  /**
   * 页面的初始数据
   */
  // 数据
  data: {

    // 数据加载
    loading: false,

    // 加载提示
    loadTips: '',
    page: 1,//当前页码
    totalpage: 100,//总页码
    content: []

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options
    });
    this.getData();
  },

  // 获取数据
  getData: function () {
    var that = this;
    if (that.data.page <= that.data.totalpage) {
      app.getLoginSession(function (session) {
        wx.request({

          // 请求地址
          url: app.api + "/wx_log_test/msg/help",

          // 请求参数
          data: {
            token: session,
            page: that.data.page,
            perpage: 10
          },

          // 请求成功
          success: function (res) {
            var res = res.data;
            if (res.msg == "OK") {
              var data = res.data;
              if (data.broadcastPage.totalPages == 0) {
                that.setData({
                  totalpage: data.broadcastPage.totalPages,
                  page: ++that.data.page,
                  loading: false,
                  loadTips: '暂无系统消息'
                })
              } else if (data.broadcastPage.totalPages == that.data.page) {
                that.setData({
                  totalpage: data.broadcastPage.totalPages,
                  page: ++that.data.page,
                  loading: false,
                  loadTips: '暂无更多消息',
                  content: that.data.content.concat(that.deallist(data.broadcastPage.content))
                })
              }
            } else {
              that.setData({
                loading: false,
                loadTips: '接口请求错误'
              })
            }
          }, fail: function (res) {
            that.setData({
              loading: true,
              errorTips: '接口请求错误'
            })
          }
        })
      })
    }

  },
  deallist(list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        console.log(item);
        item.createdOn = util.formatDate(new Date(item.createdOn), 'YYYY-MM-dd')
      })
      return list
    }
  },
  //翻页
  lower:function(res){
    this.getData();
  }
})